{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d5a12786",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-27T02:23:13.109290700Z",
     "start_time": "2024-05-27T02:23:04.372836900Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import wfdb\n",
    "import math\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from scipy import interpolate\n",
    "from scipy import signal\n",
    "import neurokit2 as nk\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6304937b",
   "metadata": {
    "scrolled": true,
    "ExecuteTime": {
     "end_time": "2024-05-27T02:23:14.504296Z",
     "start_time": "2024-05-27T02:23:13.119684600Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17596\n"
     ]
    },
    {
     "data": {
      "text/plain": "{'00001': [[7749, 7755], ['STTC', 'STTC']],\n '00002': [[6318, 6325], ['CD', 'CD']],\n '00003': [[1953, 2041], ['NORM', 'NORM']],\n '00004': [[6600, 6734], ['NORM', 'NORM']],\n '00005': [[14081, 14603], ['STTC', 'STTC']],\n '00006': [[12349], ['MI']],\n '00007': [[12163, 12356], ['NORM', 'NORM']],\n '00008': [[14334, 14361], ['MI', 'MI']],\n '00009': [[6086, 6142], ['STTC', 'STTC']],\n '00010': [[820, 825, 828], ['STTC', 'STTC', 'STTC']],\n '00011': [[18121, 18140], ['CD', 'CD']],\n '00012': [[10054, 10177], ['STTC', 'STTC']],\n '00013': [[4312, 4380], ['MI', 'MI']],\n '00014': [[12440, 13087], ['HYP', 'HYP']],\n '00015': [[10176], ['NORM']],\n '00016': [[17517, 17535], ['MI', 'MI']],\n '00017': [[2696, 3150], ['HYP', 'HYP']],\n '00018': [[10693, 10712], ['NORM', 'NORM']],\n '00019': [[14623, 14696], ['NORM', 'NORM']],\n '00020': [[10132, 10775], ['CD', 'CD']],\n '00021': [[2948, 3509], ['CD', 'CD']],\n '00022': [[14285, 14331], ['MI', 'MI']],\n '00023': [[11840, 12081, 12750], ['STTC', 'STTC', 'STTC']],\n '00024': [[874, 887], ['NORM', 'NORM']],\n '00025': [[7960, 7962], ['CD', 'CD']],\n '00026': [[1809, 1844], ['MI', 'MI']],\n '00027': [[3109, 3117], ['MI', 'MI']],\n '00028': [[2684, 3503], ['CD', 'CD']],\n '00029': [[6163, 6172], ['MI', 'MI']],\n '00030': [[824, 1057], ['NORM', 'NORM']],\n '00031': [[2030, 2092], ['STTC', 'STTC']],\n '00032': [[12060, 13104], ['NORM', 'NORM']],\n '00033': [[593, 613], ['STTC', 'STTC']],\n '00034': [[10495, 10531], ['NORM', 'NORM']],\n '00035': [[8298, 8357], ['STTC', 'STTC']],\n '00036': [[8581, 8620], ['CD', 'CD']],\n '00037': [[4894, 6331], ['CD', 'CD']],\n '00038': [[12963, 12993], ['STTC', 'STTC']],\n '00039': [[16454, 16942], ['NORM', 'NORM']],\n '00040': [[15001, 16975], ['NORM', 'NORM']],\n '00041': [[635], ['CD']],\n '00042': [[8917, 10166, 10398], ['NORM', 'NORM', 'NORM']],\n '00043': [[3270, 3295], ['STTC', 'STTC']],\n '00044': [[17607, 18280], ['NORM', 'NORM']],\n '00045': [[11724, 11728], ['CD', 'CD']],\n '00046': [[1845, 1916], ['NORM', 'NORM']],\n '00047': [[16330, 16450], ['CD', 'CD']],\n '00048': [[13695, 14383], ['CD', 'CD']],\n '00049': [[6570, 6687], ['NORM', 'NORM']],\n '00050': [[6295, 6577], ['NORM', 'NORM']],\n '00051': [[2457, 2485], ['CD', 'CD']],\n '00052': [[3176, 3181, 3204], ['STTC', 'STTC', 'STTC']],\n '00053': [[942, 948], ['HYP', 'HYP']],\n '00054': [[1815, 2096], ['CD', 'CD']],\n '00055': [[6690, 6701], ['MI', 'MI']],\n '00056': [[2198, 2211], ['NORM', 'NORM']],\n '00057': [[751, 758], ['NORM', 'NORM']],\n '00058': [[2253, 2681], ['MI', 'MI']],\n '00059': [[1232, 1237, 1240], ['CD', 'CD', 'CD']],\n '00060': [[17584, 17613], ['NORM', 'NORM']],\n '00061': [[1412, 1681], ['NORM', 'NORM']],\n '00062': [[8909, 8937], ['MI', 'MI']],\n '00063': [[8588, 8636, 8641, 8652], ['MI', 'MI', 'MI', 'MI']],\n '00064': [[4576, 4615], ['MI', 'MI']],\n '00065': [[14945, 14954], ['NORM', 'NORM']],\n '00066': [[12041, 12108], ['NORM', 'NORM']],\n '00067': [[8447, 8709], ['NORM', 'NORM']],\n '00068': [[4085, 4099], ['STTC', 'STTC']],\n '00069': [[610, 636], ['STTC', 'STTC']],\n '00070': [[16782, 16905], ['MI', 'MI']],\n '00071': [[10646, 10707], ['MI', 'MI']],\n '00072': [[2994, 2999], ['MI', 'MI']],\n '00073': [[18324, 18401], ['MI', 'MI']],\n '00074': [[14573, 14602], ['MI', 'MI']],\n '00075': [[6716, 6767], ['MI', 'MI']],\n '00076': [[16788, 16805], ['NORM', 'NORM']],\n '00077': [[8021, 8626], ['CD', 'CD']],\n '00078': [[17651, 17660], ['CD', 'CD']],\n '00079': [[17817], ['NORM']],\n '00080': [[18315, 18339], ['CD', 'CD']],\n '00081': [[12394, 12417], ['CD', 'CD']],\n '00082': [[8933, 10408], ['NORM', 'NORM']],\n '00083': [[4942, 5003], ['CD', 'CD']],\n '00084': [[5752, 6343], ['CD', 'CD']],\n '00085': [[11985, 12032], ['STTC', 'STTC']],\n '00086': [[10340, 10390], ['STTC', 'STTC']],\n '00087': [[8396, 8521], ['NORM', 'NORM']],\n '00088': [[14403, 14449], ['NORM', 'NORM']],\n '00089': [[7814, 7824], ['MI', 'MI']],\n '00090': [[12899, 13943], ['MI', 'MI']],\n '00091': [[10060, 10111], ['STTC', 'STTC']],\n '00092': [[4296, 4311], ['NORM', 'NORM']],\n '00093': [[18037, 18293], ['CD', 'CD']],\n '00094': [[6969, 8018], ['CD', 'CD']],\n '00095': [[12521, 12545], ['MI', 'MI']],\n '00096': [[12003, 12013], ['STTC', 'STTC']],\n '00097': [[18078, 18164], ['MI', 'MI']],\n '00098': [[16342, 16367, 16645], ['CD', 'CD', 'CD']],\n '00099': [[4432, 4560], ['CD', 'CD']],\n '00100': [[14290, 14544], ['MI', 'MI']],\n '00101': [[9101], ['STTC']],\n '00102': [[16501, 16530], ['CD', 'CD']],\n '00103': [[13989, 13994], ['MI', 'MI']],\n '00104': [[646, 727], ['MI', 'MI']],\n '00105': [[10621, 10774], ['NORM', 'NORM']],\n '00106': [[4635, 4643], ['NORM', 'NORM']],\n '00107': [[1141, 1356], ['NORM', 'NORM']],\n '00108': [[4644, 4647], ['NORM', 'NORM']],\n '00109': [[5725, 5743], ['NORM', 'NORM']],\n '00110': [[4730, 4750], ['STTC', 'STTC']],\n '00111': [[8450, 8475, 8501], ['STTC', 'STTC', 'STTC']],\n '00112': [[18124, 18328], ['CD', 'CD']],\n '00113': [[4614, 4656], ['MI', 'MI']],\n '00114': [[1102, 1107], ['NORM', 'NORM']],\n '00115': [[3511, 3542], ['NORM', 'NORM']],\n '00116': [[3090, 3198], ['NORM', 'NORM']],\n '00117': [[12382, 12531], ['NORM', 'NORM']],\n '00118': [[6066], ['NORM']],\n '00119': [[16801, 16840], ['NORM', 'NORM']],\n '00120': [[8644, 8691], ['NORM', 'NORM']],\n '00121': [[17878, 18075], ['NORM', 'NORM']],\n '00122': [[10747, 10799], ['NORM', 'NORM']],\n '00123': [[18088, 18093], ['STTC', 'STTC']],\n '00124': [[256, 346, 712, 730], ['CD', 'CD', 'CD', 'CD']],\n '00125': [[941, 953], ['STTC', 'STTC']],\n '00126': [[3450], ['NORM']],\n '00127': [[4053, 4095], ['MI', 'MI']],\n '00128': [[10496, 10548], ['MI', 'MI']],\n '00129': [[12331, 12391], ['CD', 'CD']],\n '00130': [[14560, 14660], ['NORM', 'NORM']],\n '00131': [[6096, 6126], ['CD', 'CD']],\n '00132': [[2705, 2769], ['STTC', 'STTC']],\n '00133': [[17866, 17868, 18017], ['HYP', 'HYP', 'HYP']],\n '00134': [[10665, 10670], ['NORM', 'NORM']],\n '00135': [[411, 763], ['NORM', 'NORM']],\n '00136': [[1924, 1947], ['MI', 'MI']],\n '00137': [[4978, 5058], ['MI', 'MI']],\n '00138': [[3441, 4431], ['NORM', 'NORM']],\n '00139': [[2046, 2059], ['NORM', 'NORM']],\n '00140': [[14477, 14639], ['MI', 'MI']],\n '00141': [[1528, 1551], ['CD', 'CD']],\n '00142': [[16396, 16462, 16570], ['CD', 'CD', 'CD']],\n '00143': [[295, 326], ['STTC', 'STTC']],\n '00144': [[16207, 16636], ['MI', 'MI']],\n '00145': [[10086, 10109], ['STTC', 'STTC']],\n '00146': [[2227, 2243], ['CD', 'CD']],\n '00147': [[12290, 12300], ['MI', 'MI']],\n '00148': [[18151], ['NORM']],\n '00149': [[1925, 1964], ['STTC', 'STTC']],\n '00150': [[14298, 14311], ['MI', 'MI']],\n '00151': [[16390, 16404, 16408], ['STTC', 'STTC', 'STTC']],\n '00152': [[9946, 10179], ['STTC', 'STTC']],\n '00153': [[3506, 3534], ['MI', 'MI']],\n '00154': [[6108, 6120], ['MI', 'MI']],\n '00155': [[14316, 14323, 14341], ['CD', 'CD', 'CD']],\n '00156': [[16374, 16565], ['STTC', 'STTC']],\n '00157': [[14126, 14555], ['MI', 'MI']],\n '00158': [[6377], ['STTC']],\n '00159': [[12288, 12643], ['STTC', 'STTC']],\n '00160': [[6938, 8007], ['CD', 'CD']],\n '00161': [[12560, 12774], ['MI', 'MI']],\n '00162': [[3297, 3303], ['NORM', 'NORM']],\n '00163': [[743, 757, 766, 907], ['STTC', 'STTC', 'STTC', 'STTC']],\n '00164': [[266, 281], ['MI', 'MI']],\n '00165': [[5695, 5697, 5710, 5724], ['NORM', 'NORM', 'NORM', 'NORM']],\n '00166': [[4025, 4090, 4097, 4103, 4110],\n  ['NORM', 'NORM', 'NORM', 'NORM', 'NORM']],\n '00167': [[1159], ['MI']],\n '00168': [[7070, 7077], ['NORM', 'NORM']],\n '00169': [[7667, 8072], ['MI', 'MI']],\n '00170': [[17973, 17989], ['CD', 'CD']],\n '00171': [[17765], ['STTC']],\n '00172': [[17850], ['NORM']],\n '00173': [[735], ['NORM']],\n '00174': [[10174], ['NORM']],\n '00175': [[645], ['NORM']],\n '00176': [[1515], ['STTC']],\n '00177': [[10538], ['STTC']],\n '00178': [[14448], ['STTC']],\n '00179': [[975], ['CD']],\n '00180': [[14541], ['MI']],\n '00181': [[6367], ['NORM']],\n '00182': [[6639], ['NORM']],\n '00183': [[17531], ['STTC']],\n '00184': [[11151], ['NORM']],\n '00185': [[14525], ['NORM']],\n '00186': [[709], ['CD']],\n '00187': [[8929], ['NORM']],\n '00188': [[6780], ['NORM']],\n '00189': [[10753], ['HYP']],\n '00190': [[18268], ['NORM']],\n '00191': [[9694], ['CD']],\n '00192': [[930], ['HYP']],\n '00193': [[6092], ['CD']],\n '00194': [[18263], ['STTC']],\n '00195': [[5051], ['STTC']],\n '00196': [[10094], ['STTC']],\n '00197': [[2845], ['NORM']],\n '00198': [[3424], ['CD']],\n '00199': [[2228], ['STTC']],\n '00200': [[4050], ['STTC']],\n '00201': [[3154], ['STTC']],\n '00202': [[17669], ['STTC']],\n '00203': [[17729], ['NORM']],\n '00204': [[13993], ['NORM']],\n '00205': [[13041], ['STTC']],\n '00206': [[12705], ['NORM']],\n '00207': [[18013], ['MI']],\n '00208': [[265], ['NORM']],\n '00209': [[633], ['CD']],\n '00210': [[1637], ['NORM']],\n '00211': [[1912], ['NORM']],\n '00212': [[17748], ['NORM']],\n '00213': [[11745], ['NORM']],\n '00214': [[2288], ['CD']],\n '00215': [[12697], ['NORM']],\n '00216': [[16781], ['CD']],\n '00217': [[656], ['STTC']],\n '00218': [[8131], ['MI']],\n '00219': [[8631], ['NORM']],\n '00220': [[18631], ['NORM']],\n '00221': [[6089], ['NORM']],\n '00222': [[6729], ['NORM']],\n '00223': [[2453], ['MI']],\n '00224': [[14293], ['NORM']],\n '00225': [[18111], ['NORM']],\n '00226': [[8366], ['MI']],\n '00227': [[4682], ['NORM']],\n '00228': [[12680], ['STTC']],\n '00229': [[3155], ['CD']],\n '00230': [[3326], ['STTC']],\n '00231': [[1168], ['CD']],\n '00232': [[6790], ['STTC']],\n '00233': [[8606], ['STTC']],\n '00234': [[8876], ['NORM']],\n '00235': [[8451], ['STTC']],\n '00236': [[14670], ['STTC']],\n '00237': [[14386], ['NORM']],\n '00238': [[12988], ['MI']],\n '00239': [[494], ['NORM']],\n '00240': [[4689], ['NORM']],\n '00241': [[3337], ['CD']],\n '00242': [[14663], ['NORM']],\n '00243': [[8067], ['NORM']],\n '00244': [[11983], ['MI']],\n '00245': [[3286], ['NORM']],\n '00246': [[6296], ['NORM']],\n '00247': [[16977], ['NORM']],\n '00248': [[10497], ['NORM']],\n '00249': [[11741], ['CD']],\n '00250': [[3048], ['NORM']],\n '00251': [[15927], ['STTC']],\n '00252': [[8042], ['NORM']],\n '00253': [[1970], ['NORM']],\n '00254': [[4587], ['NORM']],\n '00255': [[14078], ['NORM']],\n '00256': [[971], ['NORM']],\n '00257': [[15017], ['NORM']],\n '00258': [[7042], ['NORM']],\n '00259': [[3841], ['NORM']],\n '00260': [[1511], ['NORM']],\n '00261': [[1997], ['NORM']],\n '00262': [[10101], ['STTC']],\n '00263': [[4892], ['NORM']],\n '00264': [[2102], ['NORM']],\n '00265': [[17752], ['NORM']],\n '00266': [[4341], ['NORM']],\n '00267': [[17867], ['NORM']],\n '00268': [[15770], ['NORM']],\n '00269': [[10551], ['HYP']],\n '00270': [[11979], ['CD']],\n '00271': [[7970], ['HYP']],\n '00272': [[6948], ['NORM']],\n '00273': [[6737], ['NORM']],\n '00274': [[2185], ['NORM']],\n '00275': [[11958], ['STTC']],\n '00276': [[3974], ['NORM']],\n '00277': [[5992], ['NORM']],\n '00278': [[15675], ['CD']],\n '00279': [[13015], ['CD']],\n '00280': [[5736], ['CD']],\n '00281': [[6181], ['NORM']],\n '00282': [[8528], ['NORM']],\n '00283': [[6885], ['STTC']],\n '00284': [[11937], ['STTC']],\n '00285': [[7697], ['STTC']],\n '00286': [[17857], ['CD']],\n '00287': [[14975], ['STTC']],\n '00288': [[12110], ['NORM']],\n '00289': [[14265], ['STTC']],\n '00290': [[17858], ['STTC']],\n '00291': [[2262], ['CD']],\n '00292': [[8663], ['NORM']],\n '00293': [[1385], ['HYP']],\n '00294': [[2194], ['NORM']],\n '00295': [[10141], ['NORM']],\n '00296': [[9163], ['CD']],\n '00297': [[4336], ['STTC']],\n '00298': [[10601], ['CD']],\n '00299': [[11156], ['HYP']],\n '00300': [[10309], ['STTC']],\n '00301': [[3152], ['NORM']],\n '00302': [[2656], ['MI']],\n '00303': [[2711], ['NORM']],\n '00304': [[2993], ['MI']],\n '00305': [[16771], ['HYP']],\n '00306': [[1236], ['CD']],\n '00307': [[16909], ['STTC']],\n '00308': [[3258], ['CD']],\n '00309': [[683], ['NORM']],\n '00310': [[6416], ['STTC']],\n '00311': [[2256], ['STTC']],\n '00312': [[14046], ['NORM']],\n '00313': [[5825], ['NORM']],\n '00314': [[3255], ['NORM']],\n '00315': [[1047], ['NORM']],\n '00316': [[12554], ['NORM']],\n '00317': [[16547], ['NORM']],\n '00318': [[2840], ['MI']],\n '00319': [[17883], ['NORM']],\n '00320': [[9058], ['NORM']],\n '00321': [[8936], ['NORM']],\n '00322': [[856], ['CD']],\n '00323': [[6743], ['NORM']],\n '00324': [[4091], ['STTC']],\n '00325': [[10441], ['NORM']],\n '00326': [[12164], ['NORM']],\n '00327': [[2990], ['MI']],\n '00328': [[15693], ['NORM']],\n '00329': [[2837], ['NORM']],\n '00330': [[14040], ['NORM']],\n '00331': [[14690], ['NORM']],\n '00332': [[16383], ['HYP']],\n '00333': [[1977], ['CD']],\n '00334': [[16815], ['NORM']],\n '00335': [[15668], ['NORM']],\n '00336': [[16766], ['STTC']],\n '00337': [[4482], ['NORM']],\n '00338': [[1910], ['NORM']],\n '00339': [[6589], ['CD']],\n '00340': [[3119], ['NORM']],\n '00341': [[1078], ['NORM']],\n '00342': [[14355], ['MI']],\n '00343': [[6050], ['NORM']],\n '00344': [[6101], ['NORM']],\n '00345': [[16240], ['NORM']],\n '00346': [[2236], ['CD']],\n '00347': [[1554], ['STTC']],\n '00348': [[12066], ['CD']],\n '00349': [[2770], ['NORM']],\n '00350': [[17834], ['NORM']],\n '00351': [[1027], ['NORM']],\n '00352': [[1166], ['NORM']],\n '00353': [[1148], ['CD']],\n '00354': [[5020], ['HYP']],\n '00355': [[5947], ['NORM']],\n '00356': [[16580], ['STTC']],\n '00357': [[1096], ['NORM']],\n '00358': [[17836], ['NORM']],\n '00359': [[4458], ['HYP']],\n '00360': [[10708], ['STTC']],\n '00361': [[11075], ['STTC']],\n '00362': [[10770], ['NORM']],\n '00363': [[14930], ['NORM']],\n '00364': [[17549], ['MI']],\n '00365': [[10447], ['NORM']],\n '00366': [[16382], ['STTC']],\n '00367': [[14350], ['STTC']],\n '00368': [[7973], ['CD']],\n '00369': [[10443], ['NORM']],\n '00370': [[10413], ['NORM']],\n '00371': [[10920], ['NORM']],\n '00372': [[14050], ['NORM']],\n '00373': [[16489], ['MI']],\n '00374': [[3984], ['NORM']],\n '00375': [[8559], ['CD']],\n '00376': [[13846], ['NORM']],\n '00377': [[9114], ['STTC']],\n '00378': [[944], ['NORM']],\n '00379': [[16626], ['CD']],\n '00380': [[1414], ['NORM']],\n '00381': [[16239], ['STTC']],\n '00382': [[3118], ['NORM']],\n '00383': [[10545], ['NORM']],\n '00384': [[852], ['STTC']],\n '00385': [[2521], ['MI']],\n '00386': [[2021], ['NORM']],\n '00387': [[15976], ['STTC']],\n '00388': [[4285], ['NORM']],\n '00389': [[4659], ['MI']],\n '00390': [[15969], ['NORM']],\n '00391': [[12445], ['MI']],\n '00392': [[9704], ['NORM']],\n '00393': [[10084], ['CD']],\n '00394': [[9793], ['NORM']],\n '00395': [[2097], ['NORM']],\n '00396': [[1918], ['STTC']],\n '00397': [[17780], ['NORM']],\n '00398': [[10653], ['NORM']],\n '00399': [[3113], ['CD']],\n '00400': [[746], ['STTC']],\n '00401': [[3995], ['CD']],\n '00402': [[15915], ['NORM']],\n '00403': [[13904], ['STTC']],\n '00404': [[10460], ['NORM']],\n '00405': [[12764], ['NORM']],\n '00406': [[6498], ['NORM']],\n '00407': [[1184], ['NORM']],\n '00408': [[6056], ['MI']],\n '00409': [[15744], ['STTC']],\n '00410': [[1381], ['NORM']],\n '00411': [[614], ['NORM']],\n '00412': [[10135], ['MI']],\n '00413': [[3142], ['HYP']],\n '00414': [[4024], ['STTC']],\n '00415': [[2536], ['MI']],\n '00416': [[486], ['MI']],\n '00417': [[2072], ['NORM']],\n '00418': [[2491], ['HYP']],\n '00419': [[2106], ['STTC']],\n '00420': [[2176], ['CD']],\n '00421': [[18197], ['CD']],\n '00422': [[12958], ['STTC']],\n '00423': [[2235], ['MI']],\n '00424': [[10088], ['CD']],\n '00425': [[9042], ['HYP']],\n '00426': [[5979], ['NORM']],\n '00427': [[2730], ['CD']],\n '00428': [[3008], ['NORM']],\n '00429': [[2244], ['MI']],\n '00430': [[10586], ['STTC']],\n '00431': [[6756], ['HYP']],\n '00432': [[15125], ['HYP']],\n '00433': [[12728], ['NORM']],\n '00434': [[707], ['STTC']],\n '00435': [[16787], ['NORM']],\n '00436': [[5851], ['MI']],\n '00437': [[18257], ['HYP']],\n '00438': [[1198], ['HYP']],\n '00439': [[8622], ['CD']],\n '00440': [[1090], ['NORM']],\n '00441': [[14029], ['CD']],\n '00442': [[12638], ['NORM']],\n '00443': [[4544], ['NORM']],\n '00444': [[14068], ['NORM']],\n '00445': [[16265], ['NORM']],\n '00446': [[3206], ['CD']],\n '00447': [[12023], ['CD']],\n '00448': [[8975], ['CD']],\n '00449': [[2522], ['NORM']],\n '00450': [[1002], ['NORM']],\n '00451': [[1223], ['NORM']],\n '00452': [[1502], ['STTC']],\n '00453': [[3178], ['CD']],\n '00454': [[3921], ['STTC']],\n '00455': [[18338], ['NORM']],\n '00456': [[630], ['NORM']],\n '00457': [[6330], ['NORM']],\n '00458': [[18059], ['STTC']],\n '00459': [[4045], ['STTC']],\n '00460': [[12933], ['MI']],\n '00461': [[12758], ['MI']],\n '00462': [[14031], ['CD']],\n '00463': [[11735], ['CD']],\n '00464': [[3116], ['STTC']],\n '00465': [[6609], ['MI']],\n '00466': [[8720], ['NORM']],\n '00467': [[1226], ['NORM']],\n '00468': [[18638], ['CD']],\n '00469': [[10285], ['NORM']],\n '00470': [[3074], ['CD']],\n '00471': [[7989], ['NORM']],\n '00472': [[509], ['NORM']],\n '00473': [[4581], ['CD']],\n '00474': [[16266], ['CD']],\n '00475': [[996], ['MI']],\n '00476': [[15982], ['HYP']],\n '00477': [[11762], ['NORM']],\n '00478': [[8561], ['NORM']],\n '00479': [[3040], ['STTC']],\n '00480': [[8567], ['MI']],\n '00481': [[16508], ['MI']],\n '00482': [[6456], ['MI']],\n '00483': [[4909], ['HYP']],\n '00484': [[12380], ['NORM']],\n '00485': [[3299], ['CD']],\n '00486': [[1780], ['NORM']],\n '00487': [[4145], ['STTC']],\n '00488': [[17788], ['NORM']],\n '00489': [[11853], ['NORM']],\n '00490': [[14951], ['NORM']],\n '00491': [[3126], ['CD']],\n '00492': [[272], ['NORM']],\n '00493': [[2863], ['STTC']],\n '00494': [[16466], ['NORM']],\n '00495': [[4005], ['NORM']],\n '00496': [[7938], ['NORM']],\n '00497': [[3086], ['NORM']],\n '00498': [[6517], ['CD']],\n '00499': [[6069], ['NORM']],\n '00500': [[9018], ['MI']],\n '00501': [[3487], ['NORM']],\n '00502': [[649], ['NORM']],\n '00503': [[17534], ['NORM']],\n '00504': [[3273], ['NORM']],\n '00505': [[11052], ['STTC']],\n '00506': [[16473], ['HYP']],\n '00507': [[2728], ['NORM']],\n '00508': [[3417], ['CD']],\n '00509': [[17696], ['STTC']],\n '00510': [[6599], ['STTC']],\n '00511': [[17865], ['NORM']],\n '00512': [[14903], ['NORM']],\n '00513': [[15101], ['CD']],\n '00514': [[14528], ['NORM']],\n '00515': [[13967], ['STTC']],\n '00516': [[5040], ['CD']],\n '00517': [[15773], ['NORM']],\n '00518': [[2618], ['NORM']],\n '00519': [[9116], ['STTC']],\n '00520': [[6626], ['NORM']],\n '00521': [[4720], ['NORM']],\n '00522': [[9000], ['NORM']],\n '00523': [[10759], ['NORM']],\n '00524': [[2215], ['NORM']],\n '00525': [[835], ['NORM']],\n '00526': [[14929], ['MI']],\n '00527': [[8130], ['CD']],\n '00528': [[6317], ['NORM']],\n '00529': [[8984], ['NORM']],\n '00530': [[1823], ['NORM']],\n '00531': [[2850], ['STTC']],\n '00532': [[1936], ['STTC']],\n '00533': [[1524], ['HYP']],\n '00534': [[12691], ['NORM']],\n '00535': [[12040], ['NORM']],\n '00536': [[14901], ['NORM']],\n '00537': [[14719], ['MI']],\n '00538': [[7081], ['MI']],\n '00539': [[1621], ['STTC']],\n '00540': [[7999], ['STTC']],\n '00541': [[10631], ['NORM']],\n '00542': [[10603], ['NORM']],\n '00543': [[2009], ['NORM']],\n '00544': [[16993], ['NORM']],\n '00545': [[8388], ['NORM']],\n '00546': [[10705], ['NORM']],\n '00547': [[8273], ['CD']],\n '00548': [[14008], ['CD']],\n '00549': [[3830], ['NORM']],\n '00550': [[18208], ['NORM']],\n '00551': [[12343], ['STTC']],\n '00552': [[17642], ['NORM']],\n '00553': [[15987], ['NORM']],\n '00554': [[6613], ['NORM']],\n '00555': [[3243], ['MI']],\n '00556': [[12159], ['STTC']],\n '00557': [[15006], ['NORM']],\n '00558': [[10143], ['NORM']],\n '00559': [[7082], ['NORM']],\n '00560': [[15957], ['NORM']],\n '00561': [[3398], ['CD']],\n '00562': [[4976], ['NORM']],\n '00563': [[4895], ['MI']],\n '00564': [[12625], ['NORM']],\n '00565': [[6073], ['CD']],\n '00566': [[5056], ['CD']],\n '00567': [[13958], ['NORM']],\n '00568': [[313], ['HYP']],\n '00569': [[10048], ['MI']],\n '00570': [[16030], ['MI']],\n '00571': [[11943], ['NORM']],\n '00572': [[10459], ['STTC']],\n '00573': [[14463], ['NORM']],\n '00574': [[8079], ['NORM']],\n '00575': [[994], ['STTC']],\n '00576': [[10377], ['CD']],\n '00577': [[3253], ['NORM']],\n '00578': [[10990], ['NORM']],\n '00579': [[12107], ['MI']],\n '00580': [[7936], ['STTC']],\n '00581': [[8058], ['NORM']],\n '00582': [[2544], ['HYP']],\n '00583': [[4286], ['NORM']],\n '00584': [[10383], ['CD']],\n '00585': [[7036], ['CD']],\n '00586': [[3184], ['NORM']],\n '00587': [[8401], ['MI']],\n '00588': [[2309], ['NORM']],\n '00589': [[8115], ['NORM']],\n '00590': [[10126], ['STTC']],\n '00591': [[1217], ['NORM']],\n '00592': [[8135], ['STTC']],\n '00593': [[11087], ['NORM']],\n '00594': [[18234], ['HYP']],\n '00595': [[18066], ['HYP']],\n '00596': [[11002], ['NORM']],\n '00597': [[15099], ['NORM']],\n '00598': [[2150], ['HYP']],\n '00599': [[1587], ['STTC']],\n '00600': [[3512], ['HYP']],\n '00601': [[12964], ['NORM']],\n '00602': [[10692], ['NORM']],\n '00603': [[9845], ['STTC']],\n '00604': [[18322], ['NORM']],\n '00605': [[8721], ['HYP']],\n '00606': [[14444], ['NORM']],\n '00607': [[16959], ['CD']],\n '00608': [[10758], ['NORM']],\n '00609': [[6433], ['CD']],\n '00610': [[15956], ['MI']],\n '00611': [[11144], ['NORM']],\n '00612': [[14691], ['NORM']],\n '00613': [[13813], ['CD']],\n '00614': [[17638], ['CD']],\n '00615': [[2451], ['STTC']],\n '00616': [[10387], ['STTC']],\n '00617': [[500], ['HYP']],\n '00618': [[16092], ['NORM']],\n '00619': [[3122], ['MI']],\n '00620': [[15910], ['NORM']],\n '00621': [[5822], ['NORM']],\n '00622': [[7010], ['MI']],\n '00623': [[3314], ['MI']],\n '00624': [[10339], ['CD']],\n '00625': [[14971], ['NORM']],\n '00626': [[3057], ['MI']],\n '00627': [[8339], ['HYP']],\n '00628': [[16635], ['NORM']],\n '00629': [[1934], ['NORM']],\n '00630': [[4143], ['MI']],\n '00631': [[4038], ['MI']],\n '00632': [[8911], ['CD']],\n '00633': [[4556], ['NORM']],\n '00634': [[3757], ['NORM']],\n '00635': [[16778], ['CD']],\n '00636': [[15792], ['STTC']],\n '00637': [[3482], ['MI']],\n '00638': [[713], ['MI']],\n '00639': [[4555], ['NORM']],\n '00640': [[894], ['STTC']],\n '00641': [[6385], ['NORM']],\n '00642': [[17604], ['CD']],\n '00643': [[11742], ['NORM']],\n '00644': [[4068], ['CD']],\n '00645': [[8995], ['NORM']],\n '00646': [[1329], ['NORM']],\n '00647': [[8309], ['NORM']],\n '00648': [[278], ['NORM']],\n '00649': [[10749], ['NORM']],\n '00650': [[1985], ['MI']],\n '00651': [[13715], ['STTC']],\n '00652': [[5014], ['NORM']],\n '00653': [[430], ['MI']],\n '00654': [[16387], ['NORM']],\n '00655': [[2852], ['NORM']],\n '00656': [[8777], ['HYP']],\n '00657': [[12304], ['STTC']],\n '00658': [[11849], ['STTC']],\n '00659': [[16754], ['STTC']],\n '00660': [[1686], ['NORM']],\n '00661': [[4034], ['NORM']],\n '00662': [[14504], ['NORM']],\n '00663': [[6361], ['CD']],\n '00664': [[10549], ['NORM']],\n '00665': [[2625], ['NORM']],\n '00666': [[8124], ['NORM']],\n '00667': [[10493], ['HYP']],\n '00668': [[17672], ['NORM']],\n '00669': [[18307], ['STTC']],\n '00670': [[17022], ['NORM']],\n '00671': [[18369], ['NORM']],\n '00672': [[12354], ['MI']],\n '00673': [[18272], ['CD']],\n '00674': [[2093], ['HYP']],\n '00675': [[12483], ['STTC']],\n '00676': [[14924], ['STTC']],\n '00677': [[983], ['NORM']],\n '00678': [[12269], ['STTC']],\n '00679': [[3195], ['MI']],\n '00680': [[1154], ['HYP']],\n '00681': [[6008], ['NORM']],\n '00682': [[4714], ['NORM']],\n '00683': [[12129], ['NORM']],\n '00684': [[619], ['NORM']],\n '00685': [[14684], ['NORM']],\n '00686': [[12903], ['STTC']],\n '00687': [[4660], ['STTC']],\n '00688': [[9799], ['NORM']],\n '00689': [[6768], ['NORM']],\n '00690': [[3402], ['NORM']],\n '00691': [[15979], ['STTC']],\n '00692': [[3313], ['NORM']],\n '00693': [[14457], ['NORM']],\n '00694': [[6347], ['HYP']],\n '00695': [[6782], ['NORM']],\n '00696': [[14694], ['NORM']],\n '00697': [[14077], ['NORM']],\n '00698': [[9949], ['STTC']],\n '00699': [[11824], ['NORM']],\n '00700': [[4946], ['MI']],\n '00701': [[4963], ['CD']],\n '00702': [[14501], ['NORM']],\n '00703': [[10032], ['CD']],\n '00704': [[14352], ['NORM']],\n '00705': [[8499], ['CD']],\n '00706': [[2555], ['STTC']],\n '00707': [[18254], ['MI']],\n '00708': [[14579], ['NORM']],\n '00709': [[14001], ['NORM']],\n '00710': [[6027], ['CD']],\n '00711': [[15988], ['NORM']],\n '00712': [[13815], ['MI']],\n '00713': [[14035], ['MI']],\n '00714': [[8719], ['CD']],\n '00715': [[10458], ['MI']],\n '00716': [[6100], ['NORM']],\n '00717': [[14006], ['NORM']],\n '00718': [[14931], ['NORM']],\n '00719': [[18230], ['NORM']],\n '00720': [[16267], ['NORM']],\n '00721': [[650], ['STTC']],\n '00722': [[12450], ['CD']],\n '00723': [[6104], ['CD']],\n '00724': [[4026], ['CD']],\n '00725': [[1779], ['NORM']],\n '00726': [[723], ['MI']],\n '00727': [[14133], ['NORM']],\n '00728': [[14676], ['NORM']],\n '00729': [[3221], ['NORM']],\n '00730': [[10956], ['NORM']],\n '00731': [[1755], ['STTC']],\n '00732': [[4706], ['CD']],\n '00733': [[13103], ['NORM']],\n '00734': [[2465], ['NORM']],\n '00735': [[7701], ['NORM']],\n '00736': [[2742], ['CD']],\n '00737': [[12894], ['HYP']],\n '00738': [[1061], ['NORM']],\n '00739': [[12056], ['NORM']],\n '00740': [[13948], ['CD']],\n '00741': [[14005], ['MI']],\n '00742': [[14272], ['NORM']],\n '00743': [[4469], ['NORM']],\n '00744': [[3187], ['NORM']],\n '00745': [[1080], ['STTC']],\n '00746': [[6516], ['NORM']],\n '00747': [[6935], ['CD']],\n '00748': [[1130], ['NORM']],\n '00749': [[10059], ['NORM']],\n '00750': [[10542], ['CD']],\n '00751': [[310], ['MI']],\n '00752': [[3036], ['NORM']],\n '00753': [[319], ['MI']],\n '00754': [[3448], ['NORM']],\n '00755': [[6509], ['STTC']],\n '00756': [[6956], ['STTC']],\n '00757': [[3813], ['NORM']],\n '00758': [[15873], ['STTC']],\n '00759': [[8506], ['STTC']],\n '00760': [[4673], ['MI']],\n '00761': [[3808], ['CD']],\n '00762': [[14947], ['STTC']],\n '00763': [[14067], ['NORM']],\n '00764': [[16908], ['STTC']],\n '00765': [[2134], ['MI']],\n '00766': [[16632], ['MI']],\n '00767': [[9112], ['HYP']],\n '00768': [[12640], ['CD']],\n '00769': [[12597], ['CD']],\n '00770': [[7992], ['STTC']],\n '00771': [[10674], ['NORM']],\n '00772': [[11158], ['MI']],\n '00773': [[12087], ['HYP']],\n '00774': [[1012], ['MI']],\n '00775': [[17588], ['CD']],\n '00776': [[6442], ['NORM']],\n '00777': [[10585], ['NORM']],\n '00778': [[10145], ['NORM']],\n '00779': [[6340], ['NORM']],\n '00780': [[10936], ['CD']],\n '00781': [[14742], ['MI']],\n '00782': [[8773], ['MI']],\n '00783': [[3265], ['MI']],\n '00784': [[3465], ['HYP']],\n '00785': [[2012], ['MI']],\n '00786': [[575], ['STTC']],\n '00787': [[16651], ['NORM']],\n '00788': [[1547], ['CD']],\n '00789': [[472], ['NORM']],\n '00790': [[12926], ['STTC']],\n '00791': [[14533], ['NORM']],\n '00792': [[1986], ['CD']],\n '00793': [[3092], ['HYP']],\n '00794': [[14949], ['CD']],\n '00795': [[1026], ['STTC']],\n '00796': [[12904], ['HYP']],\n '00797': [[3129], ['CD']],\n '00798': [[12486], ['NORM']],\n '00799': [[18032], ['MI']],\n '00800': [[10626], ['CD']],\n '00801': [[1055], ['CD']],\n '00802': [[8136], ['STTC']],\n '00803': [[667], ['CD']],\n '00804': [[16727], ['NORM']],\n '00805': [[16081], ['STTC']],\n '00806': [[2798], ['CD']],\n '00807': [[10822], ['CD']],\n '00808': [[6176], ['NORM']],\n '00809': [[10091], ['NORM']],\n '00810': [[16655], ['NORM']],\n '00811': [[4585], ['NORM']],\n '00812': [[275], ['NORM']],\n '00813': [[4652], ['MI']],\n '00814': [[1648], ['NORM']],\n '00815': [[14521], ['HYP']],\n '00816': [[4917], ['CD']],\n '00817': [[1966], ['STTC']],\n '00818': [[12608], ['NORM']],\n '00819': [[4270], ['MI']],\n '00820': [[10076], ['STTC']],\n '00821': [[4534], ['CD']],\n '00822': [[7003], ['MI']],\n '00823': [[3429], ['NORM']],\n '00824': [[10392], ['STTC']],\n '00825': [[5093], ['MI']],\n '00826': [[4416], ['NORM']],\n '00827': [[16591], ['STTC']],\n '00828': [[16402], ['CD']],\n '00829': [[12936], ['CD']],\n '00830': [[14022], ['NORM']],\n '00831': [[10110], ['NORM']],\n '00832': [[2738], ['NORM']],\n '00833': [[17647], ['NORM']],\n '00834': [[12670], ['STTC']],\n '00835': [[826], ['MI']],\n '00836': [[1165], ['NORM']],\n '00837': [[14686], ['NORM']],\n '00838': [[4705], ['NORM']],\n '00839': [[5742], ['MI']],\n '00840': [[10494], ['HYP']],\n '00841': [[439], ['NORM']],\n '00842': [[9733], ['NORM']],\n '00843': [[15916], ['STTC']],\n '00844': [[12756], ['STTC']],\n '00845': [[6695], ['MI']],\n '00846': [[18653], ['STTC']],\n '00847': [[7001], ['MI']],\n '00848': [[6704], ['MI']],\n '00849': [[1510], ['NORM']],\n '00850': [[12667], ['MI']],\n '00851': [[3162], ['NORM']],\n '00852': [[345], ['NORM']],\n '00853': [[861], ['STTC']],\n '00854': [[2955], ['NORM']],\n '00855': [[10504], ['NORM']],\n '00856': [[3970], ['NORM']],\n '00857': [[3932], ['CD']],\n '00858': [[16234], ['MI']],\n '00859': [[18381], ['CD']],\n '00860': [[3254], ['NORM']],\n '00861': [[12720], ['NORM']],\n '00862': [[7681], ['NORM']],\n '00863': [[16082], ['MI']],\n '00864': [[1594], ['STTC']],\n '00865': [[8274], ['CD']],\n '00866': [[10984], ['MI']],\n '00867': [[14042], ['MI']],\n '00868': [[14545], ['NORM']],\n '00869': [[16966], ['CD']],\n '00870': [[652], ['MI']],\n '00871': [[915], ['CD']],\n '00872': [[11138], ['NORM']],\n '00873': [[2719], ['MI']],\n '00874': [[8392], ['STTC']],\n '00875': [[16855], ['STTC']],\n '00876': [[10638], ['STTC']],\n '00877': [[409], ['CD']],\n '00878': [[10720], ['CD']],\n '00879': [[17525], ['NORM']],\n '00880': [[16498], ['NORM']],\n '00881': [[2478], ['NORM']],\n '00882': [[16268], ['HYP']],\n '00883': [[16642], ['MI']],\n '00884': [[12514], ['NORM']],\n '00885': [[8747], ['HYP']],\n '00886': [[9122], ['CD']],\n '00887': [[12473], ['STTC']],\n '00888': [[2091], ['NORM']],\n '00889': [[3427], ['CD']],\n '00890': [[4048], ['MI']],\n '00891': [[17744], ['MI']],\n '00892': [[15118], ['NORM']],\n '00893': [[15759], ['STTC']],\n '00894': [[16996], ['NORM']],\n '00895': [[6488], ['NORM']],\n '00896': [[15054], ['NORM']],\n '00897': [[17537], ['MI']],\n '00898': [[270], ['MI']],\n '00899': [[2546], ['NORM']],\n '00900': [[17606], ['NORM']],\n '00901': [[14700], ['STTC']],\n '00902': [[16577], ['NORM']],\n '00903': [[8527], ['NORM']],\n '00904': [[6788], ['MI']],\n '00905': [[12539], ['NORM']],\n '00906': [[15941], ['STTC']],\n '00907': [[12508], ['STTC']],\n '00908': [[344], ['STTC']],\n '00909': [[10376], ['STTC']],\n '00910': [[13946], ['STTC']],\n '00911': [[17959], ['STTC']],\n '00912': [[18397], ['CD']],\n '00913': [[1940], ['STTC']],\n '00914': [[999], ['NORM']],\n '00915': [[15056], ['NORM']],\n '00916': [[4301], ['NORM']],\n '00917': [[10696], ['NORM']],\n '00918': [[10803], ['STTC']],\n '00919': [[5739], ['STTC']],\n '00920': [[12478], ['NORM']],\n '00921': [[4497], ['STTC']],\n '00922': [[17822], ['NORM']],\n '00923': [[10021], ['MI']],\n '00924': [[702], ['CD']],\n '00925': [[16537], ['NORM']],\n '00926': [[16946], ['NORM']],\n '00927': [[14119], ['CD']],\n '00928': [[14469], ['NORM']],\n '00929': [[16455], ['CD']],\n '00930': [[955], ['MI']],\n '00931': [[6462], ['NORM']],\n '00932': [[4346], ['NORM']],\n '00933': [[11078], ['CD']],\n '00934': [[17863], ['NORM']],\n '00935': [[8033], ['NORM']],\n '00936': [[8913], ['MI']],\n '00937': [[16899], ['STTC']],\n '00938': [[15947], ['NORM']],\n '00939': [[1064], ['CD']],\n '00940': [[1181], ['NORM']],\n '00941': [[11754], ['STTC']],\n '00942': [[15777], ['NORM']],\n '00943': [[12125], ['STTC']],\n '00944': [[4545], ['STTC']],\n '00945': [[7038], ['MI']],\n '00946': [[1031], ['NORM']],\n '00947': [[14634], ['NORM']],\n '00948': [[2762], ['HYP']],\n '00949': [[16471], ['NORM']],\n '00950': [[16398], ['NORM']],\n '00951': [[1152], ['NORM']],\n '00952': [[875], ['NORM']],\n '00953': [[965], ['STTC']],\n '00954': [[10348], ['NORM']],\n '00955': [[1905], ['NORM']],\n '00956': [[12036], ['CD']],\n '00957': [[6633], ['STTC']],\n '00958': [[10389], ['MI']],\n '00959': [[10748], ['NORM']],\n '00960': [[14038], ['NORM']],\n '00961': [[6071], ['NORM']],\n '00962': [[839], ['NORM']],\n '00963': [[3347], ['NORM']],\n '00964': [[2811], ['NORM']],\n '00965': [[3395], ['NORM']],\n '00966': [[2470], ['CD']],\n '00967': [[8449], ['MI']],\n '00968': [[8924], ['MI']],\n '00969': [[8047], ['NORM']],\n '00970': [[12291], ['NORM']],\n '00971': [[12330], ['NORM']],\n '00972': [[10327], ['NORM']],\n '00973': [[4041], ['NORM']],\n '00974': [[16087], ['NORM']],\n '00975': [[1685], ['NORM']],\n '00976': [[6706], ['STTC']],\n '00977': [[3159], ['NORM']],\n '00978': [[18103], ['MI']],\n '00979': [[4551], ['MI']],\n '00980': [[17974], ['NORM']],\n '00981': [[10330], ['NORM']],\n '00982': [[16467], ['MI']],\n '00983': [[914], ['MI']],\n '00984': [[8054], ['HYP']],\n '00985': [[14473], ['STTC']],\n '00986': [[14104], ['NORM']],\n '00987': [[16637], ['NORM']],\n '00988': [[14324], ['NORM']],\n '00989': [[16037], ['NORM']],\n '00990': [[410], ['NORM']],\n '00991': [[18299], ['STTC']],\n '00992': [[1871], ['NORM']],\n '00993': [[6592], ['STTC']],\n '00994': [[1553], ['NORM']],\n '00995': [[5747], ['NORM']],\n '00996': [[12586], ['STTC']],\n '00997': [[10762], ['NORM']],\n '00998': [[7087], ['NORM']],\n '00999': [[6738], ['CD']],\n '01000': [[263], ['MI']],\n ...}"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# match the patient id, label, and the ecg data id\n",
    "root = 'ptb-xl-a-large-publicly-available-electrocardiography-dataset-1.0.3/'\n",
    "records_path = root + 'records500'\n",
    "info = pd.read_csv(root + 'ptbxl_database.csv', index_col=None)\n",
    "info = info[['ecg_id', 'scp_codes', 'patient_id']]\n",
    "# choose diagnosis result with the highest probability as the final result \n",
    "def final_scp(codes):\n",
    "    dict_scp = {}\n",
    "    ls = codes.strip('{').strip('}').split(',')\n",
    "    for code in ls:\n",
    "        dict_scp[code.split(':')[0].replace(\"'\",'').replace(' ','')] = float(code.split(':')[-1])\n",
    "    max_v = max(dict_scp.values())\n",
    "    scp = [k for k, v in dict_scp.items() if v == max_v][0]\n",
    "    if scp == 'NDT':\n",
    "        scp = 'STTC'\n",
    "    elif scp == 'NST_':\n",
    "        scp = 'STTC'\n",
    "    elif scp == 'DIG':\n",
    "        scp = 'STTC'\n",
    "    elif scp == 'ISC_':\n",
    "        scp = 'STTC'\n",
    "    elif scp == 'ISCAL':\n",
    "        scp = 'STTC' \n",
    "    elif scp == 'LNGQT':\n",
    "        scp = 'STTC'\n",
    "    elif scp == 'ISCIN':\n",
    "        scp = 'STTC'\n",
    "    elif scp == 'ISCIL':\n",
    "        scp = 'STTC'\n",
    "    elif scp == 'ISCAS':\n",
    "        scp = 'STTC'\n",
    "    elif scp == 'ISCLA':\n",
    "        scp = 'STTC'\n",
    "    elif scp == 'ANEUR':\n",
    "        scp = 'STTC'\n",
    "    elif scp == 'EL':\n",
    "        scp = 'STTC'\n",
    "    elif scp == 'ISCAN':\n",
    "        scp = 'STTC'\n",
    "    elif scp == 'NORM':\n",
    "        scp = 'NORM'\n",
    "    elif scp == 'IMI':\n",
    "        scp = 'MI'\n",
    "    elif scp == 'ASMI':\n",
    "        scp = 'MI'\n",
    "    elif scp == 'ILMI':\n",
    "        scp = 'MI'\n",
    "    elif scp == 'AMI':\n",
    "        scp = 'MI'\n",
    "    elif scp == 'ALMI':\n",
    "        scp = 'MI'\n",
    "    elif scp == 'INJAS':\n",
    "        scp = 'MI'\n",
    "    elif scp == 'LMI':\n",
    "        scp = 'MI'\n",
    "    elif scp == 'INJAL':\n",
    "        scp = 'MI'\n",
    "    elif scp == 'IPLMI':\n",
    "        scp = 'MI'\n",
    "    elif scp == 'IPMI':\n",
    "        scp = 'MI'\n",
    "    elif scp == 'INJIN':\n",
    "        scp = 'MI'\n",
    "    elif scp == 'INJLA':\n",
    "        scp = 'MI'\n",
    "    elif scp == 'PMI':\n",
    "        scp = 'MI'\n",
    "    elif scp == 'INJIL':\n",
    "        scp = 'MI'\n",
    "    elif scp == 'LVH':\n",
    "        scp = 'HYP'\n",
    "    elif scp == 'LAO/LAE':\n",
    "        scp = 'HYP'\n",
    "    elif scp == 'RVH':\n",
    "        scp = 'HYP'  \n",
    "    elif scp == 'RAO/RAE':\n",
    "        scp = 'HYP'\n",
    "    elif scp == 'SEHYP':\n",
    "        scp = 'HYP'\n",
    "    elif scp == 'LAFB':\n",
    "        scp = 'CD'\n",
    "    elif scp == 'IRBBB':\n",
    "        scp = 'CD'\n",
    "    elif scp == '1AVB':\n",
    "        scp = 'CD'\n",
    "    elif scp == 'IVCD':\n",
    "        scp = 'CD'\n",
    "    elif scp == 'CRBBB':\n",
    "        scp = 'CD'\n",
    "    elif scp == 'CLBBB':\n",
    "        scp = 'CD'\n",
    "    elif scp == 'LPFB':\n",
    "        scp = 'CD'\n",
    "    elif scp == 'WPW':\n",
    "        scp = 'CD'\n",
    "    elif scp == 'ILBBB':\n",
    "        scp = 'CD'\n",
    "    elif scp == '3AVB':\n",
    "        scp = 'CD'\n",
    "    elif scp == '2AVB':\n",
    "        scp = 'CD'\n",
    "    else:\n",
    "        scp = 'others'\n",
    "    return scp  \n",
    "\n",
    "info['scp_codes'] = info['scp_codes'].apply(lambda x: final_scp(x))\n",
    "\n",
    "# drop patients with different diagnosis results for multiple trials \n",
    "id_dict = {}\n",
    "order = 1\n",
    "group = info.groupby('patient_id', sort=True)\n",
    "for _, df in group:\n",
    "    scps = df['scp_codes'].to_list()\n",
    "    if ('others' not in set(scps)) & (len(set(scps))==1):\n",
    "        id_dict['{:05d}'.format(order)] = [df['ecg_id'].to_list(), scps]\n",
    "        order += 1\n",
    "\n",
    "print(len(id_dict))\n",
    "id_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e60caa4a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-27T02:23:14.504296Z",
     "start_time": "2024-05-27T02:23:14.480188900Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "12 in [12,5,6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0b95150a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-27T02:23:14.601095900Z",
     "start_time": "2024-05-27T02:23:14.509160400Z"
    }
   },
   "outputs": [],
   "source": [
    "# resampling to 250Hz\n",
    "def resampling(array, freq, kind='linear'):\n",
    "    t = np.linspace(1, len(array), len(array))\n",
    "    f = interpolate.interp1d(t, array, kind=kind)\n",
    "    t_new = np.linspace(1, len(array), int(len(array)/freq * 250))\n",
    "    new_array = f(t_new)\n",
    "    return new_array\n",
    "\n",
    "# standard normalization \n",
    "def normalize(data):\n",
    "    scaler = StandardScaler()\n",
    "    data_norm = scaler.fit_transform(data)\n",
    "    return data_norm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "46dfb1e1",
   "metadata": {
    "scrolled": true,
    "ExecuteTime": {
     "end_time": "2024-05-27T02:53:43.225734100Z",
     "start_time": "2024-05-27T02:23:14.530957800Z"
    }
   },
   "outputs": [],
   "source": [
    "# main\n",
    "feature_path = './Feature'\n",
    "if not os.path.exists(feature_path):\n",
    "    os.mkdir(feature_path)\n",
    "\n",
    "for pid in id_dict.keys():\n",
    "    sub = []\n",
    "    for folder in os.listdir(records_path):\n",
    "        folder_path = os.path.join(records_path, folder)\n",
    "        for tri in os.listdir(folder_path):\n",
    "            if ('.hea' in tri) & (int(tri.split('.')[0].split('_')[0]) in id_dict[pid][0]):\n",
    "                tri_path = os.path.join(folder_path, tri.split('.')[0])\n",
    "                ecg_data, field = wfdb.rdsamp(tri_path)\n",
    "                trial = []\n",
    "                for ch in range(ecg_data.shape[1]):\n",
    "                    data = resampling(ecg_data[:,ch], freq=500, kind='linear')\n",
    "                    trial.append(data)\n",
    "                trial = np.array(trial).T\n",
    "                trial_norm = normalize(trial)\n",
    "                sub.append(trial_norm)\n",
    "    sub = np.array(sub)\n",
    "    sub = sub.reshape(-1, 250, sub.shape[-1])  # split 10s trial into 1s sample\n",
    "    # sub = sub.reshape(-1, 1250, sub.shape[-1])  # split 10s trial into 5s sample\n",
    "    # print(sub.shape)\n",
    "    np.save(feature_path + '/feature_{}.npy'.format(pid), sub)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "93784779",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-27T02:53:43.289981600Z",
     "start_time": "2024-05-27T02:53:43.233735400Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[    2     1]\n",
      " [    3     2]\n",
      " [    0     3]\n",
      " ...\n",
      " [    0 17594]\n",
      " [    0 17595]\n",
      " [    2 17596]]\n"
     ]
    }
   ],
   "source": [
    "# label.npy\n",
    "\n",
    "label_path = './Label'\n",
    "if not os.path.exists(label_path):\n",
    "    os.mkdir(label_path)\n",
    "    \n",
    "label = []\n",
    "for k, v in id_dict.items():\n",
    "    if 'NORM' in set(v[-1]):\n",
    "        diag = 0\n",
    "    elif  'MI' in set(v[-1]):\n",
    "        diag = 1\n",
    "    elif 'STTC' in set(v[-1]):\n",
    "        diag = 2\n",
    "    elif 'CD' in set(v[-1]):\n",
    "        diag = 3\n",
    "    else:\n",
    "        diag = 4\n",
    "    label.append([int(diag), int(k)])\n",
    "label = np.array(label)\n",
    "print(label)\n",
    "np.save(label_path + '/label.npy', label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "(2, 1250, 12)"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.load('./Feature/feature_01003.npy').shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-27T02:53:43.361086Z",
     "start_time": "2024-05-27T02:53:43.289981600Z"
    }
   },
   "id": "e9ef6c53a9f20a37"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-27T02:53:43.369535800Z",
     "start_time": "2024-05-27T02:53:43.321699500Z"
    }
   },
   "id": "ce6e4b7a81c35b4a"
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3 (ipykernel)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
